﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace eTruck.Views.Users
{
    public partial class Form : System.Web.UI.Page
    {
        protected string returnValue = string.Empty;

        BLL.Users bll = new BLL.Users();
        protected void Page_Load(object sender, EventArgs e)
        {
            string type = System.Web.HttpUtility.UrlDecode(Request["Type"]);
            if (type == "Save")
            {
                Save();
            }
            else
            {
                Exists();
            }
        }

        private void Save()
        {
            Model.Users users = new Model.Users();
            string UserID = System.Web.HttpUtility.UrlDecode(Request["UserID"]);
            users.UserName = System.Web.HttpUtility.UrlDecode(Request["UserName"]);
            users.FirstName = System.Web.HttpUtility.UrlDecode(Request["FirstName"]);
            users.LastName = System.Web.HttpUtility.UrlDecode(Request["LastName"]);
            users.EmailAddress = System.Web.HttpUtility.UrlDecode(Request["EmailAddress"]);
            users.Phone = System.Web.HttpUtility.UrlDecode(Request["Phone"]);
            users.DepartMent = System.Web.HttpUtility.UrlDecode(Request["DepartMent"]);

            try
            {
                bool result = false;
                if (UserID.Length == 0)
                {
                    //create new User
                    result = bll.Add(users);
                }
                else
                { 
                    //update User Info...
                    users.UserID =Convert.ToInt32(UserID);
                    result = bll.Update(users); 
                }
                if (result)
                {
                    returnValue = "True";
                }
                else
                {
                    returnValue = "False";
                }
            }
            catch (Exception ex)
            {
                returnValue = ex.Message;
            }
        }

        private void Exists()
        {
            string UserName = System.Web.HttpUtility.UrlDecode(Request["UserName"]);
            if (bll.Exists(UserName))
            {
                returnValue = "True";
            }
            else
            {
                returnValue = "False";
            }
        }
    }
}